//
//  WXServiceViewController.m
//  NewDataCloud
//
//  Created by luojiang on 17/07/06.
//  Copyright © 2017年 chen. All rights reserved.
//

#import "WXServiceViewController.h"
#import <SDWebImage/UIButton+WebCache.h>

#import <WXCloudModuleNetworking/WXNoNetWorkView.h>
#import <WXCloudModuleMediator/WXMediator+WXMediatorModuleNewsDetailActions.h>
#import <WXCloudModuleMediator/WXMediator+WXMediatorModuleNoNetWorkActions.h>
#import <WXCloudModuleMediator/WXMediator+WXMediatorModuleModelManager.h>

#import <WXCloudModuleNetworking/WXNetWorkConfigManager.h>
#import <WXCloudModuleNetworking/WXAppContext.h>
#import <WXCloudModuleNetworking/UIView+NoNetWorkView.h>

#import <WXCloudPluginProgressHUD/WXProgressHUD.h>

#import <ReactiveCocoa/ReactiveCocoa.h>

#import <WXCloudModuleBaseConfig/WXViewBaseConfigManager.h>
#import <WXCloudModuleBaseConfig/WXUserInfoManager.h>
#import <WXCloudModuleBaseConfig/WXSystemConfigManager.h>
#import <WXCloudModuleBaseConfig/WXSystemBaseDefine.h>
#import <WXCloudModuleBaseView/WXImageButton.h>
#import <WXCloudModuleBaseView/WXNavigationBaseView.h>
#import <WXCloudModuleCacheManager/WXCacheConfigManager.h>
#import <WXCloudModuleCustomView/WebViewJavascriptBridge.h>
#import <WXCloudModuleCategories/UIButton+ButtonCategory.h>
#import <WXCloudModuleBaseView/WXSystemNormalButton.h>



@interface WXServiceViewController ()<UIWebViewDelegate,WXNoNetWorkViewDelegate>
@property(nonatomic,strong)UIWebView *xhnWebView;

@property(nonatomic,strong)WXNavigationBaseView *titleView;

@property(nonatomic,strong)UIButton *userButton;

@property(nonatomic,strong)UIButton *refreshButton;

@property(nonatomic,strong)WebViewJavascriptBridge *bridge;

@property(nonatomic,strong)WXUserInfoManager *userInfo;

@property (nonatomic, strong) WXNoNetWorkView *noNetWorkingView;

@property (nonatomic, strong) NSURL *serviceURL;

@end

@implementation WXServiceViewController

#pragma life cycle
-(void)viewWillAppear:(BOOL)animated{
    
    _userInfo = [WXUserInfoManager sharedInstance];
    
    if ([_userInfo isLoggedIn]) {
        [self.userButton sd_setImageWithURL:[NSURL URLWithString:_userInfo.avatar] forState:UIControlStateNormal];
    }
    else{
        UIImage *leftImage = [UIImage imageNamed:@"headicon"];
        [self.userButton setImage:leftImage forState:UIControlStateNormal];
    }
    [super viewWillAppear:animated];
}

-(void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:YES];
}

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
}

- (void)viewDidDisappear:(BOOL)animated
{
    if (![_xhnWebView canGoBack]) {
        
    } else {
        [_xhnWebView goBack];
    }
    [super viewDidDisappear:animated];
}

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self.view addSubview:self.xhnWebView];
    [self.view addSubview:self.titleView];
    
    _serviceURL = [NSURL URLWithString:[NSString stringWithFormat:@"%@appid=%@",[WXNetWorkConfigManager sharedInstance].WXServiceWebURL,[WXSystemConfigManager sharedInstance].AppID]];
    
    if ([WXAppContext sharedInstance].isReachable) {
        if (self.noNetWorkingView !=nil) {
            [self.noNetWorkingView removeFromSuperview];
        }
        [self.xhnWebView loadRequest:[NSURLRequest requestWithURL:self.serviceURL]];
    } else {
        self.noNetWorkingView = [self.xhnWebView addNoNetWorkingView];
        self.noNetWorkingView.delegate = self;
    }
    [self setWebViewJavascriptBridge];

}

- (void)webViewDidFinishLoad:(UIWebView *)webView
{
    if (_noNetWorkingView != nil) {
        [_noNetWorkingView removeFromSuperview];
    }
}
#pragma mark 自定义私有方法
- (void)loadWebPage:(UIWebView*)web andURL:(NSURL *)url{
    if ([WXAppContext sharedInstance].isReachable) {
        [web loadRequest:[NSURLRequest requestWithURL:url]];
        self.noNetWorkingView.hidden=YES;
    }
    else{
        [WXProgressHUD showText:@"无网络,请检查网络设置"];
        self.noNetWorkingView.hidden=NO;
    }

}
-(void)setWebViewJavascriptBridge{
    _bridge = [WebViewJavascriptBridge bridgeForWebView:self.xhnWebView webViewDelegate:self handler:^(id data, WVJBResponseCallback responseCallback) {
        NSLog(@"ObjC received message from JS: %@", data);
        responseCallback(@"Response for message from ObjC");
        
    }];
    [_bridge registerHandler:@"openurl" handler:^(id data, WVJBResponseCallback responseCallback) {
        NSLog(@"data==%@",data);
        NSArray *array=[data componentsSeparatedByString:@"@"];
        WXBaseInteractiveModel *commonNewsModel=[[WXMediator sharedInstance]translateWebURLDataToAdapterModelWithTitle:[array objectAtIndex:0] URL:[array objectAtIndex:1]];
        
        UIViewController *targerVC = [[WXMediator sharedInstance] WXMediator_showNewsWithModelAction:commonNewsModel];
        [self.navigationController pushViewController:targerVC animated:YES];
    }];
    [_bridge registerHandler:@"clickcount" handler:^(id data, WVJBResponseCallback responseCallback) {
        NSLog(@"data==%@",data);
    }];
}

#pragma mark WXNoNetWorkViewDelegate代理方法
/** 顶部按钮代理 */
- (void)noNetWorkViewdidClickNoNetWorkTopButton
{
    UIViewController *targetVC= [[WXMediator sharedInstance] WXMediator_getNoNetWorkAction];
    [self.navigationController pushViewController:targetVC animated:YES];
}
- (void)noNetWorkViewdidClickNoNetWorkFullScreenButton{
    [self loadWebPage:self.xhnWebView andURL:self.serviceURL];
}
#pragma mark 懒加载
- (UIWebView *)xhnWebView {
    if (_xhnWebView == nil) {
        _xhnWebView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 64, WXScreenWidth,WXScreenHeight-49-64)];
        [_xhnWebView setScalesPageToFit:YES];
        [(UIScrollView *)[[_xhnWebView subviews] objectAtIndex:0] setBounces:NO];
        _xhnWebView.delegate = self;
    }
    return _xhnWebView;
}

- (WXNavigationBaseView *)titleView
{
    if (_titleView == nil) {
        _titleView = [[WXNavigationBaseView alloc]initWithTitle:@"服务"];
        _titleView.leftBtn = self.userButton;
        _titleView.rightBtn = self.refreshButton;
        [_titleView.titleLabel setTextColor:[UIColor blackColor]];
    }
    return _titleView;
}

//-(UIButton *)userButton
//{
//    if (_userButton==nil) {
//        _userButton = [[WXImageButton alloc]initImageWithOnlyImageName:WXImageButtonImageSiteCenter imageName:@"headicon" imageSize:CGSizeMake(28, 28)];
//        [_userButton setFrame:CGRectMake(0, 0,52,28)];
//        [_userButton.imageView.layer setCornerRadius:14];
//        [_userButton.imageView.layer setMasksToBounds:YES];
//        @weakify(self);
//        [[_userButton rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(id x) {
//            @strongify(self);
////            UIViewController *targetVC = [[WXMediator sharedInstance]WXMediator_getHomePageAction];
////            [self.navigationController pushViewController:targetVC animated:YES];
//        }];
//    }
//    return _userButton;
//}

-(UIButton *)refreshButton
{
    if (_refreshButton==nil) {
        _refreshButton = [WXSystemNormalButton normalBlackRefreshButton];
        [[_refreshButton rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(id x) {
            [self loadWebPage:self.xhnWebView andURL:self.serviceURL];
        }];
    }
    return _refreshButton;
}

@end
